import { parseTime } from './ruoyi'
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} phone
 * @returns {Boolean}
 */
export function isPhone(val){
  // const reg = /^1[3456789]\d{9}$/;
  const reg = new RegExp("^1[3456789]\\d{9}$");
  return reg.test(val);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  return typeof str === 'string' || str instanceof String;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

// 校验手机号码
export var validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入手机号码'))
  } else if (!isPhone(value)) {
    callback(new Error('请输入正确的手机号码'))
  } else {
    callback()
  }
}



export function integerZeroFn(value) {
  if(value==='') return ''
  let reg = /^(0|\+?[1-9][0-9]*)$/;
  let strArray = value.split("");
  let newStrs = "";
  for (let i = 0; i < strArray.length; i++) {
    if (reg.test(strArray[i])) {
      console.log(strArray[i]);
      newStrs += strArray[i];
    } else if (i > 0 && strArray[i] === "0") {
      newStrs += strArray[i];
    }
  }
  if (!(newStrs - 0 < 0)) {
    return newStrs - 0;
  } else {
    return "";
  }
}


/**
 * 
 * @param {文件大小} fileSize 
 * @param {限制大小} overSize 
 */
 export function isOverSize(fileSize,overSize) {
  var size = Math.round(fileSize / 1024 * 100) / 100;
  if (size > overSize * 1024) {
    return true;
  } else {
    return false;
  }
}

// 获取时间数组
export function getDateRang(val) {
  const now = new Date(); // 当前日期
  const nowDayOfWeek = now.getDay(); // 今天本周的第几天
  const nowDay = now.getDate(); // 当前日
  const nowMonth = now.getMonth(); // 当前月
  const nowYear = now.getFullYear(); // 当前年
  const jd = Math.ceil((nowMonth + 1) / 3);
  let startTime;
  let endTime;
  let customTime = [];
  switch (val) {
    case 'yesterday': // 昨日
      startTime = new Date(nowYear, nowMonth, nowDay - 1);
      endTime = new Date(nowYear, nowMonth, nowDay);
      break;
    case 'week': // 本周
      startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
      endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek);
      break;
    case 'pastThree': // 近 3 日
      startTime = new Date(nowYear, nowMonth, nowDay - 2);
      endTime = new Date(nowYear, nowMonth, nowDay);
      break;
    case 'pastWeek': // 近 7 日
      startTime = new Date(nowYear, nowMonth, nowDay - 6);
      endTime = new Date(nowYear, nowMonth, nowDay);
      break;
    case 'month': // 本月
      startTime = new Date(nowYear, nowMonth, 1);
      endTime = new Date(nowYear, nowMonth + 1, 0);
      break;
    case 'pastMonth': // 近 31 日
      startTime = new Date(nowYear, nowMonth, nowDay - 30);
      endTime = new Date(nowYear, nowMonth, nowDay);
      break;
    case 'quarter': // 本季度
      startTime = new Date(nowYear, (jd - 1) * 3, 1);
      endTime = new Date(nowYear, jd * 3, 0);
      break;
    case 'year': // 今年
      startTime = new Date(nowYear, 0, 1);
      endTime = new Date(nowYear, 11, 31);
      break;
    default: // 自定义时间
      customTime = val.split(' - ');
      break;
  }
  return customTime.length ? customTime : [parseTime(startTime), parseTime(endTime)];
}